#include "TChartAxesScaler.h"
#include "MainPlot.h"
#include <qwt_scale_widget.h>
#include <qwt_scale_map.h>
#include <QMouseEvent>
#include <qwt_plot_canvas.h>


//-------------------------------------

TChartAxesScaler::TChartAxesScaler( MainPlot * p ) :
    QObject(),
    plot( 0 )
{
    plot = p;
    if( plot )
        for( int i=0; i < QwtPlot::axisCnt; ++i )
            plot->axisWidget( i )->installEventFilter( this );
}

//-------------------------------------

bool TChartAxesScaler::eventFilter( QObject * obj, QEvent * e )
{
    int axis = -1;

    for( int i=0; i < QwtPlot::axisCnt; ++i )
    {
        if( obj == plot->axisWidget( i ) )
        {
            axis = i;
            break;
        }
    }

    if( axis != -1 )
    {
        QMouseEvent * me = static_cast< QMouseEvent* >( e );

        switch( e->type() )
        {
            case QEvent::MouseButtonPress:
            {
                if( dType != NON )
                    break;

                if( me->button() == Qt::LeftButton )
                {
                    QwtScaleWidget * sw = plot->axisWidget( axis );
                    QwtScaleMap sm = plot->canvasMap( plot->AxisX() );

                    xl = sm.s1();
                    xr = sm.s2();
                    w = sm.sDist();

                    sm = plot->canvasMap( plot->AxisY() );

                    yb = sm.s1();
                    yt = sm.s2();
                    h = sm.sDist();

                    QRect gc = plot->canvas()->geometry();
                    QRect gw = sw->geometry();

                    pxl = plot->transform( plot->AxisX(), xl );
                    pw = plot->transform( plot->AxisX(), xr ) - pxl;
                    pxl = pxl + gc.x() - gw.x();

                    pyt = plot->transform( plot->AxisY(), yt );
                    ph = plot->transform( plot->AxisY(), yb ) - pyt;
                    pyt = pyt + gc.y() - gw.y();

                    cursorX = me->pos().x() - pxl;
                    cursorY = me->pos().y() - pyt;

                    if( axis == plot->AxisX() )
                    {
                        if( w > 0 )
                            if( pw > 36 )
                            {
                                if( cursorX >= floor( pw/2 ) )
                                    dType = HR;
                                else
                                    dType = HL;
                            }
                    }
                    else
                    {
                        if( h > 0 )
                            if( ph > 18 )
                            {
                                if( cursorY >= floor( ph/2 ) )
                                    dType = VB;
                                else
                                    dType = VT;
                            }
                    }

                    if( dType != NON )
                    {
                        tCursor = sw->cursor();
                        sw->setCursor( Qt::PointingHandCursor );
                    }
                }
            }
            break;

            case QEvent::MouseMove:
            {
                if( dType == NON )
                    break;

                QRect gc = plot->canvas()->geometry();
                QRect gw = plot->axisWidget( axis )->geometry();

                int x = me->pos().x() + gw.x() - gc.x() - pxl;
                int y = me->pos().y() + gw.y() - gc.y() - pyt;

                bool boundChanged = false;

                switch( dType )
                {
                    case HL:
                    {
                        if( x >= pw )
                            x = pw - 1;

                        double wx = w * ( pw - cursorX ) / ( pw - x );

                        wx = limitScale( wx, w );

                        double new_xl = xr - wx;

                        plot->setAxisScale( axis, new_xl, xr );

                        boundChanged = true;
                    }
                    break;

                    case HR:
                    {
                        if( x <= 0 )
                            x = 1;

                        double wx = w * cursorX / x;

                        wx = limitScale( wx, w );

                        double new_xr = xl + wx;

                        plot->setAxisScale( axis, xl, new_xr );

                        boundChanged = true;
                    }
                    break;

                    case VB:
                    {
                        if( y <= 0 )
                            y = 1;

                        double hy = h * cursorY / y;

                        hy = limitScale( hy, h );

                        double new_yb = yt - hy;

                        plot->setAxisScale( axis, new_yb, yt );

                        boundChanged = true;
                    }
                    break;

                    case VT:
                    {
                        if( y >= ph )
                            y = ph - 1;

                        double hy = h * ( ph - cursorY ) / ( ph - y );

                        hy = limitScale( hy, h );

                        double new_yt = yb + hy;

                        plot->setAxisScale( axis, yb, new_yt );

                        boundChanged = true;
                    }
                    break;
                }

                if( boundChanged )
                    plot->replot();
            }
            break;

            case QEvent::MouseButtonRelease:
            {
                if( dType == NON )
                    break;

                if( me->button() == Qt::LeftButton )
                {
                    plot->axisWidget( axis )->setCursor( tCursor );
                    dType = NON;
                }
            }
            break;
        }
    }

    return QObject::eventFilter( obj, e );
}

//-------------------------------------



